use hex;
use rand::Rng;
use snowflake::SnowflakeIdGenerator;
use std::sync::{LazyLock, Mutex};
use uuid::Uuid;

// [LazyLock] is build-in feature similar with [lazy_static!] since rust 1.80
// IMPORTANT!!!
// Never, ever creates a new id generator everytime you need new snowflake id.
// Your entire system needs only ONE generator and shares it.
// let mut id_generator = SnowflakeIdGenerator::new(1, 1);
static SNOWFLAKE_ID_GENERATOR: LazyLock<Mutex<SnowflakeIdGenerator>> =
    LazyLock::new(|| Mutex::new(SnowflakeIdGenerator::new(1, 1)));

pub fn generate_random_u32(min_value: u32, max_value: u32) -> u32 {
    rand::rng().random_range(min_value..=max_value)
}

pub fn generate_snowflake_id() -> i64 {
    SNOWFLAKE_ID_GENERATOR.lock().unwrap().real_time_generate()
}

pub fn generate_uuid() -> Uuid {
    Uuid::new_v4()
}

pub fn generate_uuid_as_hex_string() -> String {
    let uuid = Uuid::new_v4();

    hex::encode(uuid.as_bytes())
}
